package com.example.love_margin.Untils;

import com.example.love_margin.Mapper.PetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Component
public class forDelete {
    @Autowired
    PetMapper petMapper;

    //全局变量，方便更改图片删除目录（不包含图片位置文件夹）
    @Value("${web.imgURL2}")
    private String imgURL2;

    public boolean DeleteList(List<String> URLs){
        String KI = "";
        for (int i = 0;i < URLs.size();i++){
            System.out.println(imgURL2);
            if (URLs.get(i) != null) {
                KI = imgURL2 + URLs.get(i);
                File test = new File(KI);
                if (test.exists()) {
                    try {
                        test.delete();
                        System.out.println("此文件删除");
                    }catch (Exception e){
                        System.out.println("文件未存在");
                        return false;
                    }
                }else {
                    return true;
                }
            } else {
                System.out.println("没有图片");
            }
        }
        return true;
    }

    public List<String> DeleteList2(List<String> URLs){
        String KI = "";
        List<String> URL = new ArrayList<>();
        for (int i = 0;i < URLs.size();i++){
            System.out.println(imgURL2);
            if (URLs.get(i) != null) {
                String[] KK = URLs.get(i).split(",");
                List<String> MK = new ArrayList<>();
                MK = Arrays.asList(KK);
                if(MK.size()> 0){
                    URL.addAll(MK);
                }
            }
        }
        return URL;
    }

}
